//
//  ZKLineChartPopView.m
//  Private_Object
//
//  Created by pro on 16/10/6.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "ZKLineChartPopView.h"

@implementation ZKLineChartPopView
{
    // data
    CGFloat _ratio;             // 箭头位置的比例
    CGFloat _corner;            // 圆角
    CGFloat _arrowH;            // 箭头高度
    
    // view
    UILabel *_textLabel;        // 文本视图
}
- (instancetype)initWithFrame:(CGRect)frame arrowPositionRatio:(CGFloat)ratio {
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor clearColor];
        _arrowH     = 8;            // 箭头高度
        _corner     = 8;            // 圆角
        _ratio      = ratio;        // 箭头位置（-1 ~ 1）,负数表示在上面
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    
    [super drawRect:rect];
    
    CGFloat width = rect.size.width;
    CGFloat height = rect.size.height;
    CGFloat remain = 1;
    UIBezierPath *path = [UIBezierPath bezierPath];
    path.lineWidth = remain * 2;
    
    if (_ratio > 0) {
        [path moveToPoint:CGPointMake(width * _ratio, height - remain)];
        [path addLineToPoint:CGPointMake(width * _ratio - _arrowH, height - _arrowH)];
        [path addLineToPoint:CGPointMake(_corner, height - _arrowH)];
        [path addArcWithCenter:CGPointMake(_corner, height - _arrowH - (_corner - remain)) radius:_corner - remain startAngle:M_PI / 2 endAngle:M_PI clockwise:YES];
        [path addLineToPoint:CGPointMake(remain, _corner)];
        [path addArcWithCenter:CGPointMake(_corner, _corner) radius:_corner - remain startAngle:M_PI endAngle:M_PI * 1.5 clockwise:YES];
        [path addLineToPoint:CGPointMake(width - _corner, remain)];
        [path addArcWithCenter:CGPointMake(width - _corner, _corner) radius:_corner - remain startAngle:M_PI * 1.5 endAngle:M_PI * 2 clockwise:YES];
        [path addLineToPoint:CGPointMake(width - remain, height - _arrowH - (_corner - remain))];
        [path addArcWithCenter:CGPointMake(width - _corner, height - _arrowH - (_corner - remain)) radius:_corner - remain startAngle:0 endAngle:M_PI / 2 clockwise:YES];
        [path addLineToPoint:CGPointMake(width * _ratio + _arrowH, height - _arrowH)];
        [path closePath];
    } else {
        CGFloat ratio = -_ratio;
        [path moveToPoint:CGPointMake(width * ratio, remain)];
        [path addLineToPoint:CGPointMake(width * ratio - _arrowH, remain + _arrowH)];
        [path addLineToPoint:CGPointMake(_corner, remain + _arrowH)];
        [path addArcWithCenter:CGPointMake(_corner, _arrowH + _corner) radius:_corner - remain startAngle:M_PI * 1.5 endAngle:M_PI clockwise:NO];
        [path addLineToPoint:CGPointMake(remain, height - _corner)];
        [path addArcWithCenter:CGPointMake(_corner, height - _corner) radius:_corner - remain startAngle:M_PI endAngle:M_PI * 0.5 clockwise:NO];
        [path addLineToPoint:CGPointMake(width - _corner, height - remain)];
        [path addArcWithCenter:CGPointMake(width - _corner, height - _corner) radius:_corner - remain startAngle:M_PI * 0.5 endAngle:0 clockwise:NO];
        [path addLineToPoint:CGPointMake(width - remain, _arrowH + _corner)];
        [path addArcWithCenter:CGPointMake(width - _corner, _arrowH + _corner) radius:_corner - remain startAngle:0 endAngle:M_PI * 1.5 clockwise:NO];
        [path addLineToPoint:CGPointMake(width * ratio + _arrowH, remain + _arrowH)];
        [path closePath];
    }
    
    [HTB_THEME_COLOR setFill];
    [path fill];
//    [[UIColor cyanColor] setStroke];
//    [path stroke];
    
    _textLabel = [[UILabel alloc] initWithFrame:self.bounds];
    _textLabel.font = FONT(14);
    _textLabel.adjustsFontSizeToFitWidth = YES;
    _textLabel.text = _content;
    _textLabel.textColor = [UIColor whiteColor];
    _textLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:_textLabel];
    if (_ratio > 0) {
        _textLabel.y = (self.height - _arrowH) / 2;
    } else {
        _textLabel.y = (self.height - _arrowH) / 2 + _arrowH;
    }
}


- (void)setArrowPosition:(CGPoint)arrowPosition {
    
    if (_ratio > 0) {
        self.bottom = arrowPosition.y - 20;
        self.right = arrowPosition.x + (1 - _ratio) * self.width;
    } else {
        self.top = arrowPosition.y + 20;
        self.right = arrowPosition.x + (1 - -_ratio) * self.width;
    }
}
- (void)setContent:(NSString *)content {
    _content = content;
}
@end
